package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @Autowired
    private VodUploadClient vodUploadClient;
    @Autowired
    private Credential cred;

    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        try {
            //将上传文件保存到本地 TODO 后续添加定时任务,定期清理目录下使用完毕的文件
            String filePath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(),file);
            //构造上传请求对象
            VodUploadRequest request = new VodUploadRequest();
            request.setMediaFilePath(filePath);
            //调用上传方法,传入接入点地域及上传请求
            VodUploadResponse response = vodUploadClient.upload(vodConstantProperties.getRegion(), request);
            if(response != null){
                String fileId = response.getFileId();
                String mediaUrl = response.getMediaUrl();
                Map<String,String> map = Map.of("mediaFileId", fileId, "mediaUrl", mediaUrl);
                return map;
            }
            return null;
        } catch (Exception e) {
            log.error("上传音频失败", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        try{
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] fileIds1 = {mediaFileId};
            req.setFileIds(fileIds1);

            // 返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
            // 解析响应结果
            TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
            if(resp != null){
                MediaInfo[] mediaInfoSet = resp.getMediaInfoSet();
                MediaInfo mediaInfo = mediaInfoSet[0];
                //获取基础信息 分类
                String type = mediaInfo.getBasicInfo().getType();
                //获取元信息 大小、时长
                Long size = mediaInfo.getMetaData().getSize();
                Float duration = mediaInfo.getMetaData().getDuration();
                //封装属性
                trackMediaInfoVo.setType(type);
                trackMediaInfoVo.setSize(size);
                trackMediaInfoVo.setDuration(duration);
            }
            return trackMediaInfoVo;
        } catch (TencentCloudSDKException e) {
            log.error("获取媒体资源:{}信息失败",mediaFileId,e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteMedia(String mediaFileId) {
        try{
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(mediaFileId);

            // 返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            log.error("删除:点播平台文件失败:{}", e);
            throw new RuntimeException(e);
        }
    }
}
